#ifndef AUDIOPLAYER_H
#define AUDIOPLAYER_H
#include <QtMultimedia/QAudioOutput>
#include <QMutex>
#include <QDebug>

class QIODevice;
class QAudioOutput;

class AudioPlayer
{
public:
    static AudioPlayer* instance();

    void StartAudioPlayer();
    void StopAudioPlayer();

    int getFreeSpace();
    void setVolume(int value);
    void setMuted(bool muted);
    bool WriteAudioData(const char* dataBuff, int size);

    void playAudio(bool play);

    void setSamleRate(int value) {m_sampleRate = value;}
    void setSampleSize(int value) {m_sampleSize = value;}
    void setChannel(int value) {m_channel = value;}

private:
    int m_sampleRate = 48000;
    int m_sampleSize = 16;
    int m_channel = 2;

    QIODevice* m_pIODevice = nullptr;
    QAudioOutput* m_pAudioOutput = nullptr;

    static AudioPlayer* m_pInstance;
    static QMutex m_audioPlayMutex;

    AudioPlayer();

    class Garbage {
    public:
        ~Garbage() {
            if (AudioPlayer::m_pInstance != nullptr) {
                delete AudioPlayer::m_pInstance;
                AudioPlayer::m_pInstance = nullptr;
            }
        }
    };

    static Garbage m_garbage;
};

#endif // AUDIOPLAYER_H
